package com.bamboo.tool.entity;

import cn.hutool.core.collection.CollectionUtil;
import com.bamboo.tool.util.StringUtil;
import com.intellij.psi.PsiElement;
import com.intellij.psi.impl.source.javadoc.PsiDocParamRef;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.javadoc.PsiDocTag;
import com.intellij.psi.javadoc.PsiDocTagValue;
import com.intellij.psi.javadoc.PsiDocToken;
import lombok.Data;
import org.apache.tools.ant.taskdefs.Java;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Create by GuoQing
 * Date 2022/8/3 17:44
 * Description
 */
@Data
public class JavaDocComment {

    private Map<String, List<JavaDocTag>> tags;
    /**
     * 描述
     */
    private String description;

    public static JavaDocComment buildJavaDocComment(PsiDocComment psiDocComment) {
        if (Objects.isNull(psiDocComment)) {
            return null;
        }
        JavaDocComment javaDocComment = new JavaDocComment();
        PsiDocTag[] tags = psiDocComment.getTags();
        if (tags.length > 0) {
            Map<String, List<JavaDocTag>> listMap = Arrays.stream(tags).map(e -> {
                JavaDocTag javaDocTag = new JavaDocTag();
                javaDocTag.setName(e.getName());
                PsiElement[] dataElements = e.getDataElements();
                StringBuilder values = new StringBuilder();
                for (PsiElement psiElement : dataElements) {
                    if (psiElement instanceof PsiDocParamRef) {
                        javaDocTag.setParamName(StringUtil.replace(psiElement.getText(), "'", "`"));
                    } else {
                        values.append(psiElement.getText());
                    }

                }
                javaDocTag.setValue(StringUtil.replace(values.toString(), "'", "`"));
                return javaDocTag;
            }).collect(Collectors.groupingBy(e -> e.getName(), Collectors.toList()));
            javaDocComment.setTags(listMap);
        }

        StringBuilder commentStringBuilder = new StringBuilder();
        PsiElement[] descriptionElements = psiDocComment.getDescriptionElements();
        for (PsiElement descriptionElement : descriptionElements) {
            if (descriptionElement instanceof PsiDocToken) {
                commentStringBuilder.append(StringUtil.replace(descriptionElement.getText(), "'", "`")).append("\n");
            }
        }
        String description = commentStringBuilder.toString().trim();

        javaDocComment.setDescription(description);
        if (StringUtil.isEmpty(description) && tags.length < 1) {
            return null;
        }
        return javaDocComment;
    }

    public String getOneJavaDocTag(String key) {
        if (tags == null) {
            return null;
        }
        List<JavaDocTag> javaDocTags = tags.get(key);
        if (CollectionUtil.isNotEmpty(javaDocTags)) {
            return javaDocTags.get(0).getValue();
        }
        return null;
    }

    public List<JavaDocTag> getJavaDocTags(String key) {
        if (tags == null) {
            return null;
        }
        List<JavaDocTag> javaDocTags = tags.get(key);

        return javaDocTags;
    }
}
